package com.zbkj.crmeb.merchant.admin.controller;

import com.crmeb.core.common.CommonResult;
import com.constants.Constants;
import com.crmeb.core.utils.RedisUtil;
import com.zbkj.crmeb.enums.MarkupRateTypeEnum;
import com.zbkj.crmeb.merchant.api.controller.ApiBaseController;
import com.zbkj.crmeb.store.model.MarkupRate;
import com.zbkj.crmeb.store.request.MarkupRateRequest;
import com.zbkj.crmeb.store.service.MarkupRateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;


/**
 * 商户设置加价
 */
@Slf4j
@RestController("MerMarkupRateController")
@RequestMapping("api/chant/mer")
@Api(tags = "商户")
public class MerMarkupRateController extends AdminBaseController {

    @Autowired
    private MarkupRateService markupRateService;
    @Autowired
    private RedisUtil redisUtil;

    /**
     * 商户加价
     * @return
     */
    @ApiOperation(value = "商户加价列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<MarkupRate> merchantList(@Validated MarkupRateRequest request, HttpServletRequest servletRequest){
        request.setMerId(getMerId(servletRequest));
        request.setType(MarkupRateTypeEnum.MERCHANT_商户加价率.getType());
        MarkupRate markupRate = markupRateService.selectMarkupRate(request);
        return CommonResult.success(markupRate);
    }

    /**
     * 添加商户加价
     */
    @ApiOperation(value = "添加渠道商户")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public CommonResult<String> addMarkupRate(@RequestBody @Validated MarkupRate entity, HttpServletRequest servletRequest){
        entity.setMerId(getMerId(servletRequest));
        entity.setType(MarkupRateTypeEnum.MERCHANT_商户加价率.getType());
        if(markupRateService.save(entity)){
            return CommonResult.success();
        }else{
            return CommonResult.failed();
        }
    }

    /**
     * 修改商户加价
     */
    @ApiOperation(value = "修改渠道商户")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public CommonResult<String> updateMerchant(@RequestBody @Validated  MarkupRate entity,HttpServletRequest request){
        if(markupRateService.updateById(entity)){
            redisUtil.remove(Constants.MERCHANT_RATIO+getMerId(request));
            return CommonResult.success();
        }else{
            return CommonResult.failed();
        }
    }


}
